<template>
  <div class="app-container">


    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item v-if="isgly" label="付费商户" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户订单号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付平台订单号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="支付平台订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付商品名" prop="body">
        <el-input
          v-model="queryParams.body"
          placeholder="请输入支付商品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付额" prop="totalFee">
        <el-input
          v-model="queryParams.totalFee"
          placeholder="请输入支付额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="tradeState">
        <el-input
          v-model="queryParams.tradeState"
          placeholder="请输入支付状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable
                        v-model="queryParams.payTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column v-if="isgly" label="付费商户" align="center" prop="account"/>
      <el-table-column label="商户订单号" align="center" prop="outTradeNo"/>
      <el-table-column label="支付平台订单号" align="center" prop="transactionId"/>
      <el-table-column label="支付商品名" align="center" prop="body"/>
      <el-table-column label="支付额" align="center" prop="totalFee"/>
      <el-table-column label="支付状态" align="center" prop="tradeState"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付平台" align="center" prop="payType"/>
      <el-table-column label="更多信息" align="center" prop="moreInfo"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微信支付订单号/支付宝交易号(凭证号)" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入微信支付订单号/支付宝交易号(凭证号)"/>
        </el-form-item>
        <el-form-item label="支付产品名" prop="body">
          <el-input v-model="form.body" placeholder="请输入支付产品名"/>
        </el-form-item>
        <el-form-item label="支付额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入支付额"/>
        </el-form-item>
        <el-form-item label="支付成功时间" prop="payTime">
          <el-date-picker clearable
                          v-model="form.payTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择支付成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更多信息" prop="moreInfo">
          <el-input v-model="form.moreInfo" placeholder="请输入更多信息"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/payOrder/payOrder";

  export default {
    name: "Order",
    data() {
      return {
        // 遮罩层
        isgly: false,
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 支付订单表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          account: null,
          outTradeNo: null,
          transactionId: null,
          body: null,
          totalFee: null,
          tradeState: null,
          payTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          account: [
            {required: true, message: "付费账户不能为空", trigger: "blur"}
          ],
          body: [
            {required: true, message: "支付产品名不能为空", trigger: "blur"}
          ],
          totalFee: [
            {required: true, message: "支付额不能为空", trigger: "blur"}
          ],
          tradeState: [
            {
              required: true,
              message: "SUCCESS--支付成功 REFUND--转入退款 NOTPAY--未支付 CLOSED--已关闭 REVOKED--已撤销(刷卡支付) USERPAYING--用户支付中 PAYERROR--支付失败(其他原因，如银行返回失败) ACCEPT--已接收，等待扣款不能为空",
              trigger: "blur"
            }
          ],
          createTime: [
            {required: true, message: "订单生成时间不能为空", trigger: "blur"}
          ],
          payType: [
            {required: true, message: "1：weiChatPay   2：aliPay  3：ohter不能为空", trigger: "change"}
          ]
        }
      };
    },
    created() {
      var roles = this.$store.state.user.roles;
      for (var i = 0; i < roles.length; i++) {
        if (roles[i] == 'admin' || roles[i] == 'yewuadmin') {
          this.isgly = true
          break
        }
      }
      this.getList();
    },
    methods: {
      /** 查询支付订单列表 */
      getList() {
        this.loading = true;
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          account: null,
          outTradeNo: null,
          transactionId: null,
          body: null,
          totalFee: null,
          tradeState: null,
          createTime: null,
          payTime: null,
          moreInfo: null,
          payType: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.outTradeNo)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加支付订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const outTradeNo = row.outTradeNo || this.ids
        getOrder(outTradeNo).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改支付订单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.outTradeNo != null) {
              updateOrder(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOrder(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const outTradeNos = row.outTradeNo || this.ids;
        this.$modal.confirm('是否确认删除支付订单编号为"' + outTradeNos + '"的数据项？').then(function () {
          return delOrder(outTradeNos);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/order/export', {
          ...this.queryParams
        }, `order_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
